package com.cptravel.activity.order;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseFragment;
import com.core.eventbus.EventBus;
import com.core.eventbus.EventBus.SubscriberChangeListener;
import com.core.util.B5MActivityHelper;
import com.core.widget.pulltorefresh.PullToRefreshListView;
import com.cptravel.R;
import com.cptravel.adapter.OrderAdapter;
import com.cptravel.api.OrderRequest;
import com.cptravel.api.OrderResponse;
import com.cptravel.bean.Order;
import com.cptravel.tool.EventNames;
import com.cptravel.tool.UserInfoManage;

import java.util.ArrayList;
import java.util.List;

public class OrderFragment extends B5MBaseFragment implements OnClickListener, SubscriberChangeListener
{
    private EditText et_keyword;
    private Button bt_query;
    private PullToRefreshListView mPullToRefreshListView;
    private OrderRequest mOrderRequest;
    private OrderAdapter mOrderAdapter;
    private List<Order> all, temp;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.order;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        bt_left = (Button) containerView.findViewById(R.id.bt_head_left);
        bt_right = (Button) containerView.findViewById(R.id.bt_head_right);
        tv_title = (TextView) containerView.findViewById(R.id.tv_head_title);
        bt_left.setVisibility(View.GONE);
        tv_title.setText("我的订单");
        et_keyword = (EditText) containerView.findViewById(R.id.et_keyword);
        bt_query = (Button) containerView.findViewById(R.id.bt_query);
        mPullToRefreshListView = (PullToRefreshListView) containerView.findViewById(R.id.pull_refresh_list);
        bt_query.setOnClickListener(this);
        mPullToRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                // TODO Auto-generated method stub
                Order order = temp.get((int) arg3);
                Bundle bundle = new Bundle();
                bundle.putSerializable("order", order);
                B5MActivityHelper.startActivity(mActivity, OrderDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mOrderAdapter = new OrderAdapter(mActivity, new ArrayList<Order>());
        all = new ArrayList<Order>();
        temp = new ArrayList<Order>();
        mPullToRefreshListView.setAdapter(mOrderAdapter);
        mOrderRequest = new OrderRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                OrderResponse orderResponse = (OrderResponse) b5mBaseResponse;
                all.clear();
                temp.clear();
                all.addAll(orderResponse.orders);
                temp.addAll(orderResponse.orders);
                mOrderAdapter.setList(temp);
                mOrderAdapter.notifyDataSetChanged();
            }
        }, mProgressBar);
        UserInfoManage.getInstance().checkIsLogined(mActivity);
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.bt_query:
                queryOrder();
                break;
            default:
                break;
        }
    }

    private void queryOrder()
    {
        // TODO Auto-generated method stub
        String keyword = et_keyword.getText().toString().trim();
        temp.clear();
        if (!TextUtils.isEmpty(keyword))
        {
            int size = all.size();
            for (int i = 0; i < size; i++)
            {
                Order order = all.get(i);
                if (order.orderid.indexOf(keyword) != -1 || keyword.indexOf(order.pname) != -1)
                {
                    temp.add(order);
                }
            }
        }
        else
        {
            temp.addAll(all);
        }
        mOrderAdapter.notifyDataSetChanged();
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.LOGIN_SUCCESS, this);
        EventBus.getInstance().subscribe(EventNames.ORDER_UPDATE_SUCCESS, this);
        EventBus.getInstance().subscribe(EventNames.ORDER_ORDER_SUCCESS, this);
        if (all.isEmpty()&&!mOrderRequest.isStarted())
        {
            mOrderRequest.start();
        }
    }

    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.LOGIN_SUCCESS, this);
        EventBus.getInstance().unSubscribe(EventNames.ORDER_UPDATE_SUCCESS, this);
        EventBus.getInstance().unSubscribe(EventNames.ORDER_ORDER_SUCCESS, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName, Object notificateContent)
    {
        // TODO Auto-generated method stub
        if (EventNames.LOGIN_SUCCESS.equals(notificationName))
        {
            if (temp.isEmpty())
            {
                if (!all.isEmpty())
                {
                    temp.addAll(all);
                    mOrderAdapter.notifyDataSetChanged();
                }
                else
                {
                    mOrderRequest.start();
                }
            }
        }
        else if (EventNames.ORDER_UPDATE_SUCCESS.equals(notificationName))
        {
            mOrderRequest.start();
        }
        else if (EventNames.ORDER_ORDER_SUCCESS.equals(notificationName))
        {
            mOrderRequest.start();
        }
    }
}
